<?php
defined("_VALID_ACCESS") || die('Direct access forbidden');
class SCTV_Sms extends Module{
 	public function body() {
        $theme = $this->pack_module('Base/Theme');
        $message = $_POST['message'];
        if(!empty($message))
        {
            $my_record = CRM_ContactsCommon::get_my_record();
            $message = CRM_ContactsCommon::contact_format_default($my_record, true) . ': ' . $message;

            $dbconn=pg_connect("host=localhost port=5432 dbname=sms user=postgres password=admin");
            foreach($_POST['companies'] as $key=>$contacts)
            {
                foreach($contacts['contacts'] as $key=>$value)
                {
                    $contact = Utils_RecordBrowserCommon::get_record('contact', $key);
                    if(!empty($contact) && !empty($contact['mobifone'])){
                        if($key == 119){
                            $message = mb_convert_encoding($message, 'US-ASCII','UTF-8');
                            //$message = htmlentities($message, 'ENT_COMPAT','UTF-8');
                        }
                        SCTV_SmsCommon::send_out($dbconn, $contact['mobifone'], $message, 'MANUAL', null, $contact['last_name'] . ' ' . $contact['first_name']);
                    }
                }
            }
            pg_close($dbconn);
        }

        $form = $this->init_module('Libs/QuickForm', null, rand());
        $form->reset();

        // begin tree

        // Ban giam doc id = 9
        $the_company = Utils_RecordBrowserCommon::get_record('company', 9);
        if ( $the_company != NULL ) {
            $contacts = Utils_RecordBrowserCommon::get_records('contact', array('company_name'=>$the_company['id'], '!mobifone'=>''), array(), array('first_name' => 'ASC', 'last_name' => 'ASC'), array(), true);
            if(!empty($contacts)){
                $form->addElement('checkbox', 'companies[' . $the_company['id'] . ']', $the_company['company_name'], $the_company['company_name'] . ' (' . count($contacts) . ')',
                    array(
                        "onchange"=>"
                            var company = jQuery(this);
                            var li = company.parent().parent();
                            li.find('ul input').each(function(i, object){ object.checked = company.prop('checked')});
                        "
                    )
                );
                $number = 0;
                foreach($contacts as $contact){
                    if ( $contact[':active'] == 1 ) {
                        $number++;
                        $full_name = $contact['last_name'] . ' ' . $contact['first_name'] . ' - ' . $contact['mobifone'];
                        $form->addElement('checkbox', 'companies[' . $the_company['id'] . '][contacts][' . $contact['id'] . ']', $full_name, $number. '.' .$full_name);
                    }
                }
            }
        }

        // Phong ke toan - tong hop id = 7
        $the_company = Utils_RecordBrowserCommon::get_record('company', 7);
        if ( $the_company != NULL ) {
            $contacts = Utils_RecordBrowserCommon::get_records('contact', array('company_name'=>$the_company['id'], '!mobifone'=>''), array(), array('first_name' => 'ASC', 'last_name' => 'ASC'), array(), true);
            if(!empty($contacts)){
                $form->addElement('checkbox', 'companies[' . $the_company['id'] . ']', $the_company['company_name'], $the_company['company_name'] . ' (' . count($contacts) . ')',
                    array(
                        "onchange"=>"
                            var company = jQuery(this);
                            var li = company.parent().parent();
                            li.find('ul input').each(function(i, object){ object.checked = company.prop('checked')});
                        "
                    )
                );
                $number = 0;
                foreach($contacts as $contact){
                    if ( $contact[':active'] == 1 ) {
                        $number++;
                        $full_name = $contact['last_name'] . ' ' . $contact['first_name'] . ' - ' . $contact['mobifone'];
                        $form->addElement('checkbox', 'companies[' . $the_company['id'] . '][contacts][' . $contact['id'] . ']', $full_name, $number . '.' . $full_name);
                    }
                }
            }
        }

        // Bo phan ki thuat manager = 182
        $the_company = Utils_RecordBrowserCommon::get_record('company', 8);
        if ( $the_company != NULL ) {
            $crits = array(
                '!mobifone' => '',
                'manager' => 182
            );
            $order = array(
                'first_name' => 'ASC',
                'last_name' => 'ASC'
            );

            $contacts = Utils_RecordBrowserCommon::get_records('contact', $crits, array(), $order, array(), true);
            if(!empty($contacts)){
                $form->addElement('checkbox', 'companies[' . $the_company['id'] . ']', $the_company['company_name'], $the_company['company_name'] . ' (' . (count($contacts) + 1) . ')',
                    array(
                        "onchange"=>"
                            var company = jQuery(this);
                            var li = company.parent().parent();
                            li.find('ul input').each(function(i, object){ object.checked = company.prop('checked')});
                        "
                    )
                );
                $the_contact = Utils_RecordBrowserCommon::get_record('contact', 182);
                $full_name = $the_contact['last_name'] . ' ' . $the_contact['first_name'] . ' - ' . $the_contact['mobifone'];
                $form->addElement('checkbox', 'companies[' . $the_company['id'] . '][contacts][' . $the_contact['id'] . ']', $full_name, '1.' . $full_name);
                $number = 1;
                foreach($contacts as $contact){
                    if ( $contact[':active'] == 1 ) {
                        $number++;
                        $full_name = $contact['last_name'] . ' ' . $contact['first_name'] . ' - ' . $contact['mobifone'];
                        $form->addElement('checkbox', 'companies[' . $the_company['id'] . '][contacts][' . $contact['id'] . ']', $full_name, $number . '.' . $full_name);
                    }
                }
            }
        }

        // Khu vuc
        $list_area = Utils_RecordBrowserCommon::get_records('SCTV_Area', array(), array(), array(':id' => 'ASC'), array(), true);
        foreach($list_area as $the_area) {
            if ($the_area[':active'] == 1) {
                $crits = array(
                    'manager' => $the_area['manager'],
                    '!mobifone' => ''
                );
                $order = array(
                    'first_name' => 'ASC',
                    'last_name' => 'ASC'
                );
                $contacts = Utils_RecordBrowserCommon::get_records('contact', $crits, array(), $order, array(), true);
                if(!empty($contacts)){
                    $form->addElement('checkbox', 'companies[area-' . $the_area['id'] . ']', $the_area['name'], $the_area['name'] . ' (' . (count($contacts) + 1) . ')',
                        array(
                            "onchange"=>"
                            var area = jQuery(this);
                            var li = area.parent().parent();
                            li.find('ul input').each(function(i, object){ object.checked = area.prop('checked')});
                        "
                        )
                    );
                    $the_contact = Utils_RecordBrowserCommon::get_record('contact', $the_area['manager']);
                    $full_name = $the_contact['last_name'] . ' ' . $the_contact['first_name'] . ' - ' . $the_contact['mobifone'];
                    $form->addElement('checkbox', 'companies[area-' . $the_area['id'] . '][contacts][' . $the_contact['id'] . ']', $full_name, '1.' . $full_name);
                    $number = 1;
                    foreach($contacts as $contact){
                        if ( $contact[':active'] == 1 ) {
                            $number++;
                            $full_name = $contact['last_name'] . ' ' . $contact['first_name'] . ' - ' . $contact['mobifone'];
                            $form->addElement('checkbox', 'companies[area-' . $the_area['id'] . '][contacts][' . $contact['id'] . ']', $full_name, $number . '.' . $full_name);
                        }
                    }
                }
            }
        }

        // end tree

        /*$companies = Utils_RecordBrowserCommon::get_records('company', array(), array(), array('company_name' => 'ASC'), array(), true);
        foreach($companies as $company){
            $contacts = Utils_RecordBrowserCommon::get_records('contact', array('company_name'=>$company['id'], '!mobifone'=>''), array(), array('first_name' => 'ASC', 'last_name' => 'ASC'), array(), true);
            if ( count($contacts) > 0) echo count($contacts); $number = 0;
            if(!empty($contacts)){
                $e = $form->addElement('checkbox', 'companies[' . $company['id'] . ']', $company['company_name'], $company['company_name'],
                    array(
                        "onchange"=>"
                            var company = jQuery(this);
                            var li = company.parent().parent();
                            li.find('ul input').each(function(i, object){ object.checked = company.prop('checked')});
                        "
                    )
                );
                foreach($contacts as $contact){
                    if ( $contact[':active'] == 1 ) {$number++;
                        $full_name = $contact['last_name'] . ' ' . $contact['first_name'] . ' - ' . $contact['mobifone'];
                        $e = $form->addElement('checkbox', 'companies[' . $company['id'] . '][contacts][' . $contact['id'] . ']', $full_name, $full_name);
                    }
                }echo ':' . $number . '<br />';
            }
        }*/

        $form->addElement('textarea', 'message', 'Message', array('style'=>'height:100px;font-size:16px', 'maxlength'=>"160"));
        $form->addElement('submit', 'submit', __('Send'), array('style'=>'height:30px;width:60px;'));

        $form->assign_theme('form', $theme);

        ob_start();
        $rb = $this->init_module('Utils/RecordBrowser','SCTV_Sms');
        $rb->set_header_properties(array(
            'content'=>array('width'=>40),
            'receivers'=>array('width'=>40),
            'Date'=>array('width'=>10),
            'Time'=>array('width'=>10)
        ));
        $rb->disable_pdf();
        $rb->disable_export();
        $rb->disable_actions();
        //$rb->set_defaults(array('requirement'=>$arg['id']));
        $rpm = array(array(), array(), array('date'=>'desc', 'time'=>'desc'));
        $this->display_module($rb,$rpm,'show_data');
        $table = ob_get_contents();
        ob_end_clean();
        $theme->assign('history', $table);
        $theme->display('default');
    }

    public function requirement_addon($arg){
        $rb = $this->init_module('Utils/RecordBrowser','SCTV_RequirementSms');
        $rb->set_header_properties(array(
            'in_out'=>array('width'=>5),
            'employee'=>array('width'=>15),
            'phone_number'=>array('width'=>6),
            'content'=>array('width'=>50),
            'Date'=>array('width'=>12),
            'Time'=>array('width'=>12)
        ));
        $rb->set_defaults(array('requirement'=>$arg['id']));
        $rpm = array(array('requirement'=>$arg['id']), array(), array('date'=>'desc', 'time'=>'desc'));
        $this->display_module($rb,$rpm,'show_data');
    }
}
?>